/**********************************************************************
* File: loadGame.cpp
* ID  : 
* Purpose: Provide functions to load Map, Item and Character.
* Date   : 2004/04/20
* Author : T.S.Yo
* Description: 
*    This file contains methods to load game data from files
**********************************************************************/
//---------------------------------------------------------------------
// loadMap(string mapFileName, Map &map1)
//---------------------------------------------------------------------
//    Given "mapFileName" where the data of locations stored,
//    this function read the data from the file and store into
//    a Map class, map1.
//---------------------------------------------------------------------
void ifEngine::loadMap(const char *mapFileName)
{
    // infstream constructor opens file
    cout << "Open [" << mapFileName << "] to import map data.\n";
    ifstream inMapFile( mapFileName );
    
    if (!inMapFile)
    {
        cout << "Map File could not be opened." << endl;
        exit(1);
    }

    // Create tmperal variables
    Location tmpLocation;
    string tmp,
           tmpName,
           tmpSDescr,
           tmpLDescr;
    int tmpID,
        tmpExit[6],
        tmpPoint;
    // Start to load data
    cout << "Loading Map:";
    while(!inMapFile.eof())
    {
        cout << ".";
        inMapFile >> tmp >> tmpID;        // Read ID
        for (int i=0; i<6; i++)           // Read Exits
            inMapFile >> tmpExit[i];
        inMapFile >> tmpPoint;            // Read Point
        getline(inMapFile,tmp);
        getline(inMapFile,tmpName);       // Read Name
        getline(inMapFile,tmpSDescr);     // Read Short Description            

    // Read Long-Description till meet a line '.'
        tmpLDescr = "";
        getline(inMapFile,tmp);
        while(tmp!=".")
        {
            tmpLDescr += (tmp+"\n");
            getline(inMapFile,tmp);
        }

    // Skip blank line
        getline(inMapFile,tmp);
                
    // Display the loaded data on screen
//        cout << tmpID << endl;
//        cout << tmpExit[0] << tmpExit[1] << tmpExit[2]
//             << tmpExit[3] << tmpExit[4] << tmpExit[5] << endl;
//        cout << tmpPoint << endl;
//        cout << tmpName << endl;
//        cout << tmpSDescr << endl;
//        cout << tmpLDescr << endl;

    // Set read data to a Location object
        tmpLocation.setID(tmpID);
        tmpLocation.setExit(tmpExit[0],'n');
        tmpLocation.setExit(tmpExit[1],'s');
        tmpLocation.setExit(tmpExit[2],'e');
        tmpLocation.setExit(tmpExit[3],'w');
        tmpLocation.setExit(tmpExit[4],'u');
        tmpLocation.setExit(tmpExit[5],'d');
        tmpLocation.setName(tmpName);
        tmpLocation.setPoint(tmpPoint);
        tmpLocation.setSDescr(tmpSDescr);
        tmpLocation.setLDescr(tmpLDescr);
        tmpLocation.setAccessed(false);
    
    // Insert the location to the map
        map1.addLoc(tmpLocation);
    }
    inMapFile.close();
    cout << "Map loaded" << endl;
}
//---------------------------------------------------------------------


//---------------------------------------------------------------------
// loadItem(const string &itemFileName)
//---------------------------------------------------------------------
//    Given "itemFileName" where the data of items stored,
//    this function read the data from the file and insert them into
//    each location.
//---------------------------------------------------------------------
void ifEngine::loadItem(const char *itemFileName)
{
    // infstream constructor opens file
    cout << "Open [" << itemFileName << "] to import item data.\n";
    ifstream inItemFile( itemFileName );
    
    if (!inItemFile)
    {
        cout << "Item File could not be opened." << endl;
        exit(1);
    }

    // Create tmperal variables
    Item tmpItem;
    string tmp,
           tmpName,
           tmpDescr;
    int tmpLocation,
        tmpPoint,
        tmpWeight;
    // Start to load data
    cout << "Loading Items:";
    while(!inItemFile.eof())
    {
        cout << ".";
        getline(inItemFile,tmpName);       // Read Name
        inItemFile >> tmpLocation;         // Read Location
        inItemFile >> tmpPoint;            // Read Point
        inItemFile >> tmpWeight;           // Read Weight
        getline(inItemFile,tmp);           // Skip end line

    // Read description till meet a line '.'
        getline(inItemFile,tmp);
        tmpDescr = "";
        while(tmp!=".")
        {
            tmpDescr += (tmp+"\n");
            getline(inItemFile,tmp);
        }

    // Skip blank line
        getline(inItemFile,tmp);
                
    // Display the loaded data on screen
//        cout << tmpName << endl;
//        cout << tmpLocation << endl;
//        cout << tmpPoint << endl;
//        cout << tmpWeight << endl;
//        cout << tmpDescr << endl;

    // Set data to a Item object
        tmpItem.setName(tmpName);
        tmpItem.setLocation(tmpLocation);
        tmpItem.setPoint(tmpPoint);
        tmpItem.setWeight(tmpWeight);
        tmpItem.setDescription(tmpDescr);
        tmpItem.setAccessed(false);
    
    // Insert the Items to the map
        if (tmpLocation == -1)
            player.addItem(tmpItem);
        else
            map1.addItem(tmpLocation,tmpItem);
    }
    inItemFile.close();
    cout << "Items loaded" << endl;
}
//---------------------------------------------------------------------

